ROOTCFLAGS = $(shell root-config --cflags)
ROOTLIBS = $(shell root-config --libs)
ROOTGLIBS = $(shell root-config --glibs)

CXX = g++

SOURCES = TEvent.cxx TTrackerHit.cxx
HEADERS = $(SOURCES:.cxx=.h)
OBJECTS = $(SOURCES:.cxx=.o)
name := g4test
G4TARGET := $(name)
TARGETDIR = $(G4WORKDIR)/tmp/$(G4SYSTEM)/$(G4TARGET)
EVENTSO = $(TARGETDIR)/libTEvent.so

CXXFLAGS = -Wall -O2 $(ROOTCFLAGS) -g
CXXLIBS = $(ROOTLIBS)
CXXLIBS += $(EVENTSO)

TESTSRC = test.cc
TARGET = r
OBJ = test.o


#all:    TEvent.cxx TEvent.h
all:
	rm -f TEventDict.cxx TEventDict.h TTrackerHitDict.cxx TTrackerHitDict.h
	rm -f *.o
	rootcint TEventDict.cxx -c -p -O2 -Wall -fPIC $(ROOTCFLAGS) $(HEADERS) 
	rootcint TTrackerHitDict.cxx -c -p -O2 -Wall -fPIC $(ROOTCFLAGS) TTrackerHit.h
	@echo $(EVENTSO)
	$(CXX) -O2 -Wall -fPIC `root-config --cflags`  -c  TEvent.cxx
	$(CXX) -O2 -Wall -fPIC `root-config --cflags`  -c  TEventDict.cxx
	$(CXX) $(CXXFLAGS)  -c  TTrackerHit.cxx
	$(CXX) $(CXXFLAGS)  -c  TTrackerHitDict.cxx
	@if test -d $(TARGETDIR); then echo "Target directory $(TARGETDIR) existed"; else mkdir $(TARGETDIR); fi
	$(CXX) -shared -o $(TARGETDIR)/libTTrackerHit.so `root-config --ldflags` $(ROOTLIBS) $(CXXFLAGS) TTrackerHitDict.o TTrackerHit.o
	$(CXX) -shared -o $(EVENTSO) `root-config --ldflags` $(ROOTLIBS) $(CXXFLAGS) TEventDict.o TEvent.o TTrackerHit.o

.PHONY:    clean distclean

test:
	$(CXX) -c $(CXXFLAGS) $(TESTSRC)
	$(CXX) -o $(TARGET) $(OBJ) $(CXXLIBS)

clean:    
	rm -f libTEvent.so
	rm -f TEventDict.cxx TEventDict.h
	rm -f *.o $(TARGET) *.root

again:    
	make clean
	make

.PHONY: all clean
